import { hasScope } from '@/utils/auth'

const mediaUrl = process.env.VUE_APP_FILE_PREFIX

// 图片过滤器
export const mediaFilter = function(value) {
  let path = ''
  if (!value) {
    return null
  } else if (typeof value === 'string') {
    path = value
  } else if (value.path) {
    path = value.path
  }

  if (path.indexOf(mediaUrl) > -1) {
    return path
  } else if (/^https:|http:/.test(path) || path.indexOf(':') > -1) {
    return path
  } else {
    return `${mediaUrl}/${path}`
  }
}

export const hideCost = function(value) {
  if (hasScope('stock_cost')) {
    return value || '0.00'
  } else {
    return '**'
  }
}

const myFilter = {}
myFilter.install = function(Vue) {
  Vue.filter('media', mediaFilter)
  Vue.filter('hideCost', hideCost)
}

export default myFilter
